package com.subang.function.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.framework.web.service.UserIntegralService;
import com.subang.function.domain.SbArticle;
import com.subang.function.domain.SbStudyMark;
import com.subang.function.domain.SysUserCollect;
import com.subang.function.service.ISysUserCollectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbExamReview;
import com.subang.function.service.ISbExamReviewService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 公考模考复盘Controller
 * 
 * @author subei
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/function/review")
public class SbExamReviewController extends BaseController
{
    @Autowired
    private ISbExamReviewService sbExamReviewService;

    @Autowired
    private ISysUserCollectService sysUserCollectService;

    @Autowired
    private UserIntegralService userIntegralService;

    /**
     * 查询公考模考复盘列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SbExamReview sbExamReview)
    {
        startPage();
        List<SbExamReview> list;
        String orderBy = sbExamReview.getOrderBy();
        Long myCollect = sbExamReview.getMyCollect();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(("reviewTime").equals(orderBy)){
            list = sbExamReviewService.selectSbExamReviewListByReviewTime(sbExamReview);
        }else if(("likes").equals(orderBy)){
            list = sbExamReviewService.selectSbExamReviewListByLikes(sbExamReview);
        }else if(myCollect != null && 1L == myCollect){
            SysUserCollect sysUserCollect = new SysUserCollect();
            sysUserCollect.setUserId(user.getUserId());
            sysUserCollect.setCollectType("模考复盘");
            List<SysUserCollect> myCollectList = sysUserCollectService.selectSysUserCollectList(sysUserCollect);

            List<Long> sbExamReviewIds = new ArrayList<>();
            for (SysUserCollect collect : myCollectList) {
                sbExamReviewIds.add(collect.getCollectId());
            }
            list = sbExamReviewService.selectSbExamReviewListByIds(sbExamReviewIds);
        }else{
            sbExamReview.setUserid(user.getUserId());
            list = sbExamReviewService.selectSbExamReviewList(sbExamReview);
        }

        return getDataTable(list);
    }

    /**
     * 导出公考模考复盘列表
     */
    @Log(title = "公考模考复盘", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbExamReview sbExamReview)
    {
        List<SbExamReview> list = sbExamReviewService.selectSbExamReviewList(sbExamReview);
        ExcelUtil<SbExamReview> util = new ExcelUtil<SbExamReview>(SbExamReview.class);
        util.exportExcel(response, list, "公考模考复盘数据");
    }

    /**
     * 获取公考模考复盘详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sbExamReviewService.selectSbExamReviewById(id));
    }

    /**
     * 新增公考模考复盘
     */
    @Log(title = "公考模考复盘", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbExamReview sbExamReview)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sbExamReview.setUserid(user.getUserId());
        sbExamReview.setUserNickname(user.getNickName());
        sbExamReview.setReviewTime(new Date());
        if (toAjax(sbExamReviewService.insertSbExamReview(sbExamReview)).isSuccess()) {
            return userIntegralService.updateUserIntegral("mockTest");
        } else {
            return AjaxResult.error("新增复盘失败");
        }
    }

    /**
     * 修改公考模考复盘
     */
    @Log(title = "公考模考复盘", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbExamReview sbExamReview)
    {
        return toAjax(sbExamReviewService.updateSbExamReview(sbExamReview));
    }

    /**
     * 删除公考模考复盘
     */
    @Log(title = "公考模考复盘", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sbExamReviewService.deleteSbExamReviewByIds(ids));
    }

    @GetMapping("/groupStatistics/{groupId}")
    public AjaxResult getGroupExamStatistics(@PathVariable String groupId) {
        return success(sbExamReviewService.selectGroupExamStatistics(groupId));
    }

    @GetMapping("/personal/{userId}")
    public AjaxResult getGroupPersonal(@PathVariable String userId) {
        // 先获取模考信息
        AjaxResult result = success(sbExamReviewService.getGroupPersonal(userId));
        
        if (result.isSuccess()) {
            // 积分更新，但不影响返回结果
            userIntegralService.updateUserIntegral("viewExam");
            // 返回模考信息
            return result;
        } else {
            return AjaxResult.error("查看模考失败");
        }
    }


    @GetMapping("/getFirepowerRanking")
    public AjaxResult getFirepowerRanking(@RequestParam String rankType) {
        Map<String, Object> params = new HashMap<>();
        params.put("rankType", rankType);
        return success(sbExamReviewService.getFirepowerRanking(params));
    }
}
